
import * as PIXI from 'pixi.js';
import Text from 'components/text';
import {findGameApp} from 'common/utils';
const { Container } = PIXI;

export default class FadeOutText {
  constructor({parent, onEnd = () => {}, ...props}) {
    this.parent = parent;

    this.container = new Container();

    this._renderTextList(props, onEnd);
  }

  _renderTextList = (data, onEnd) => {
    const { timeout, fontSize, color, lineHeight = data.fontSize, top } = data;
    let delay = 0;
    data.list.forEach((item, index) => {

      let textMod = null;

      for (let i = 1; i <= item.length; i++) {

        const time = (i + 1) * timeout + delay;

        if (i === item.length) {
          delay += time;
        }

        setTimeout(() => {
          if (textMod) this.container.removeChild(textMod);
          const showText = item.slice(0, i);
          textMod = new Text({value: showText, top: top + index * lineHeight, fontSize, fill: color});
          textMod.x = (window.CFG.view.width - textMod.width) / 2;
          this.container.addChild(textMod);
          if (index === data.list.length - 1 && i === item.length) {
            onEnd();
          }
        }, time);

      }

      // item.isLineFirst && firstTextNum++;
      //
      // const time = (index + 1) * timeout + (lineTimeout - timeout) * firstTextNum;
      //
      // setTimeout(() => {
      //   let text = new Text(item);
      //   this.container.addChild(text);
      //   if (index === data.list.length - 1) {
      //     onEnd();
      //   }
      // }, time);
    });
  }
}
